package com.itheima.health.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.CheckGroupDao;
import com.itheima.health.dao.CheckItemDao;
import com.itheima.health.dao.OrderDao;
import com.itheima.health.dao.SetMealDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.SetMealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author spt
 * @description 套餐SEVICE实现类
 * @date 2019/9/26
 **/
@Service
@Slf4j
public class SetMealServiceImpl implements SetMealService {
    @Autowired
    private SetMealDao setMealDao;

    @Autowired
    private CheckGroupDao checkGroupDao;

    @Autowired
    private CheckItemDao checkItemDao;

    @Autowired
    private OrderDao orderDao;
    @Override
    public void add(Setmeal setmeal, Integer[] checkgroupIds) {
        log.info("[套餐-添加]data:{},checkgroupIds:{}",setmeal,checkgroupIds);
        // 调用DAO数据入库
        // 插入基本数据
        setMealDao.insert(setmeal);
        // 插入关联数据
        for (Integer checkgroupId : checkgroupIds) {
            setMealDao.insertSetMealAndCheckGroup(setmeal.getId(),checkgroupId);
        }

    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //设置分页参数
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        //DAO查询数据
        Page<Setmeal> page = setMealDao.selectByCondition(queryPageBean.getQueryString());
        //封装返回值
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public List<Setmeal> findAll() {
        List<Setmeal> setmeals = setMealDao.findAll();
        return setmeals;
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @Override
    public Setmeal findByID(Integer id) {

        return setMealDao.selectById(id);
    }


    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
   /* @Override
    public Setmeal findByID(Integer id) {
      //创建存放checkGroup对象的集合
        List<CheckGroup> checkGroupList = new ArrayList<>();
        //调用setMealDao 通过id查询返回Setmeal对象
        Setmeal setmeal = setMealDao.findById(id);
        //通过查询setmeal和checkgroup关联表传入在setmeal获取的id 返回检查组id的集合
        List<Integer> groupId = setMealDao.selectSetMealAndCheckGroup(setmeal.                                                  getId());
        //对返回的检查组id 进行遍历
        for (Integer gId : groupId) {
            //通过调用checkGroupDao查询 遍历得到的检查组id 放CheckGroup对象
            CheckGroup checkGroup =  checkGroupDao.selectById(gId);
            //通过在检查组中的id 查询对应检查项的ids 返回检查项id 的集合
            List<Integer> checkItemIds = checkGroupDao.selectCheckItemIdsByCheckGroupId(checkGroup.getId());
            //创建存放 CheckItem对象的集合
            List<CheckItem> checkItemList = new ArrayList<>();
            //对检查项的ids 遍历拿到每一个id
            for (Integer checkItemId : checkItemIds) {
                //拿到每一个具体然后通过id进行查询 返回一个CheckItem对象
                CheckItem checkItem = checkItemDao.selectById(checkItemId);
                //把对象 添加到检查组的集合中
                checkItemList.add(checkItem);
                //把得到的检查组集合属性设置到检查组里  组包含多个项
                checkGroup.setCheckItems(checkItemList);
            }

            checkGroupList.add(checkGroup);
        }
        // 把得到的检查组集合 设置到 套餐中       套餐包含检查组
        setmeal.setCheckGroups(checkGroupList);
        return setmeal;
    }
 }*/
    @Override
    public Map<String, Object> getSetmealReport() {
        Map<String,Object> date = new HashMap<>();
        List<Setmeal> all = setMealDao.findAll();
        List<String> setmealNames = new ArrayList<>();
        List<Map> setmealCount = new ArrayList<>();
        for (Setmeal setmeal : all) {
            Map<String,Object> setmealCountMap = new HashMap<>();
            setmealNames.add(setmeal.getName());
            Integer count=orderDao.setmealCount(setmeal.getId());
            setmealCountMap.put("name",setmeal.getName());
            setmealCountMap.put("value",count);
            setmealCount.add(setmealCountMap);
        }
        date.put("setmealNames",setmealNames);
        date.put("setmealCount",setmealCount);
        return date;
    }


    @Override
    public List<Map<String, Object>> findSetmealCount() {
        return setMealDao.findSetmealCount();
    }

    @Override
    public Setmeal findById(Integer id) {
        //调用DAO查询数据
        return setMealDao.selectById(id);
    }
}
